﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using KeyboardArg;
using System.IO;

namespace SoftKeyBoard
{
    public partial class PhoneNumberKeyboard : Form
    {
        private System.Media.SoundPlayer player;

        private IntPtr _parentHandle = IntPtr.Zero;

        public PhoneNumberKeyboard()
        {
            InitializeComponent();
        }

        public PhoneNumberKeyboard(IntPtr handle)
        {
            _parentHandle = handle;
            InitializeComponent();
        }

        public const int USER_MESSAGE_OK = 0x0405;//用户自定义消息的开始数值
        public const int USER_MESSAGE_CLEAR = 0x0406;//用户自定义消息的开始数值
     
        [DllImport("user32.dll")]
        public static extern void PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);

        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        private const int WS_EX_TOOLWINDOW = 0x00000080;
        private const int WS_EX_NOACTIVATE = 0x08000000;
        public event NumberClickEventHandler NumberKeyClick;
        public event EventHandler BackspaceKeyClick;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= (WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW);
                //cp.Parent = IntPtr.Zero; // Keep this line only if you used UserControl
                return cp;
                //return base.CreateParams;
            }
        }

        /// <summary>
        /// 启动键盘点击事件
        /// </summary>
        /// <param name="e"></param>
        protected void OnNumberKeyClick(NumberCharClickEventArgs e)
        {
            if (NumberKeyClick != null)
            {
                NumberKeyClick(this, e);
            }
        }

        /// <summary>
        /// 启动退格事件
        /// </summary>
        /// <param name="e"></param>
        protected void OnBackspaceClick(EventArgs e)
        {
            if (BackspaceKeyClick != null)
            {
                BackspaceKeyClick(this, e);
            }
        }   

        private void buttonNumber_Click(object sender, EventArgs e)
        {
            string key = ((Button)sender).Text.Trim();
            OnNumberKeyClick(new NumberCharClickEventArgs(key));
            player.Play();
        }

        private void buttonBackspace_Click(object sender, EventArgs e)
        {
            OnBackspaceClick(new EventArgs());
            player.Play();
        }

        private void SoftPhoneNumberKeyboard_Load(object sender, EventArgs e)
        {
            string path = @"Sound/click.wav";
            if (!File.Exists(path))
                return;
            player = new System.Media.SoundPlayer(path); 
        }

        private void ButtonOK_Click(object sender, EventArgs e)
        {
            if (_parentHandle != IntPtr.Zero)
            {
                this.Hide();
                int res = SendMessage(_parentHandle, USER_MESSAGE_OK, 0, 0);
                if (res == 0x00)
                {
                    this.Show();
                }
                else if (res == 0xff)
                {
                    this.Show();
                }
            }
        }
    }
}
